#pragma once

#include "treewalker.h"

class Uic;
class Driver;
class CTextStream;
struct Option;

typedef struct 
{
	int x;
	int y;
	int width;
	int height;
	int act;
}PropertyRect;

struct WriteInitialization : public TreeWalker
{
	typedef std::vector<DomProperty*> DomPropertyList;
	typedef std::map<CString, DomProperty*> DomPropertyMap;

	WriteInitialization(Uic *uic);

	void acceptUI(DomUI *node);

	void acceptWidget(DomWidget *node);

	void acceptProperty(DomProperty *node);

	void acceptLayout(DomLayout *node);

	void acceptLayoutItem(DomLayoutItem *node);

	void acceptConnection(DomConnection *connection);

private:
	void writeTemplateVar();

	void writeConstructFunction(DomUI *node, bool destroy = false);

	void writeInitDialog();

	void writeCreate(DomWidget *pWidget);

	void writeCreateTemplate(DomWidget *pWidget);

	void rectFromVBoxLayout(std::map<DomLayout*, PropertyRect>::iterator iter,
		std::map<DomLayoutItem*, PropertyRect>& result);

	void rectFromHBoxLayout(std::map<DomLayout*, PropertyRect>::iterator iter,
		std::map<DomLayoutItem*, PropertyRect>& result);

	void rectFromGridLayout(std::map<DomLayout*, PropertyRect>::iterator iter,
		std::map<DomLayoutItem*, PropertyRect>& result);

	bool rowItemsFixable(std::vector<DomLayoutItem*> rowItems);

	//bool rowItemFixable(DomLayoutItem* rowItem);

private:
	Uic				*_uic;
	Driver			*_driver;
	CTextStream		&_output;
	const Option	&_option;
	CString			_indent;
	CString			_dindent;
	bool			_stdsetdef;

	int				_resourceId;
	CString			_generateClassName;
	CString			_mainWidgetClass;
	
	std::map<DomWidget*, PropertyRect>		_widgetRectSet;
	std::map<DomLayout*, PropertyRect>		_layoutRectSet;	
	std::map<CString, DomWidget*>			_registWidget;
	std::stack<DomWidget*>					_widgetChain;
	std::stack<DomLayout*>					_layoutChain;
};
